package scraper

import (
	"context"
	"database/sql"

	"github.com/prometheus/client_golang/prometheus"
	"go.uber.org/zap"
)

var netSQL = `SELECT SEND_BYTES_PER_SECOND , RECEIVE_BYTES_PER_SECOND FROM V$SYSTEMINFO`

var scraperNetworkSendBpsDesc = prometheus.NewDesc(
	prometheus.BuildFQName(Namespace, Exporter, "collecto_network_sendbps"),
	"Collect Network Send Byte_per_second",
	[]string{"collector"},
	nil,
)
var scraperNetworkRcvBpsDesc = prometheus.NewDesc(
	prometheus.BuildFQName(Namespace, Exporter, "collect_network_rcvbps"),
	"Collect Network Receive Byte_per_second",
	[]string{"collector"},
	nil,
)

type ScrapeNet struct{}

func (ScrapeNet) Name() string {
	return "net_rate"
}

func (ScrapeNet) Usage() string {
	return "Current Network Rate of kingbaseDB"
}

func (ScrapeNet) Scraper(ctx context.Context, db *sql.DB, ch chan<- prometheus.Metric, logger *zap.Logger) error {
	send, rcv := 0, 0
	err := db.QueryRow(netSQL).Scan(&send, &rcv)
	if err != nil {
		logger.Sugar().Errorf("Failed to scrape network info: %v", err)
		return err
	}
	ch <- prometheus.MustNewConstMetric(
		scraperNetworkSendBpsDesc, prometheus.GaugeValue, float64(send), "connection",
	)
	ch <- prometheus.MustNewConstMetric(
		scraperNetworkRcvBpsDesc, prometheus.GaugeValue, float64(rcv), "connection",
	)
	return nil
}

var _ DBScraper = (*ScrapeNet)(nil)
